<template>
    <div class="container" v-cloak>
        <div class="blurBg" :style="{background:'url('+blurBg+') 0 0 no-repeat'}"></div>
        <div class="dataInfo">
            <input type="text" placeholder="请输入作品标题" maxlength="20" class="opusTitle" v-model="title">
            <div class="bottomLine"></div>
            <div class="inputModel">
                <div class="inputTitle Semibold font13">作品点评</div>
                <textarea class="titleInput" ref="textarea" placeholder="请输入作品内容" v-model="content" maxlength="2000"></textarea>
                <div class="inputLimit"><span v-text="content.length"></span>/2000</div>
            </div>
            <div class="uploadModel">
                <div class="uploadTitle Semibold font13" flex="cross:bottom">
                    <div>作品展示</div>
                    <div class="font10 uploadNum">(<span v-text="uploadList.length" :style="{color:uploadList.length>0?'#1dc6b4':''}"></span>/9)</div>
                </div>
                <ul class="uploadList">
                    <li class="uploadAdd" flex="main:center cross:center" v-if="uploadList.length<9"
                         @click="showAdd()">
                        <img src="../../images/addIcon.png" class="addIcon"/>
                    </li>
                    <li flex v-for="(pic,index) in uploadList" class="picesWrap" @click="showPicesSwiper(uploadList,index)">
                        <img :src="imagedomain+pic.imgUrl+'?imageView2/0/w/224/format/jpeg'" v-if="pic.old && pic.type==1" class="picesItem"/>
                        <img :src="imagedomain+pic.imgUrl+'?vframe/jpeg/offset/1/w/224'" v-if="pic.old && pic.type==3" class="picesItem"/>
                        <img :src="pic.datathumbnail" v-if="!pic.old" class="picesItem"/>
                        <div class="audioWrapper" v-if="!pic.old && pic.datatype==2">
                            <img src="../../images/audioBg.png"/>
                            <div class="audioTime" flex="main:center cross:center">
                                <span v-text="pic.datatime>=60?parseInt(pic.datatime/60):0" flex-box="0"></span>
                                <span flex-box="0">:</span>
                                <span v-text="parseInt(pic.datatime%60)>=10?parseInt(pic.datatime%60):'0'+parseInt(pic.datatime%60)" flex-box="0"></span>
                            </div>
                        </div>
                        <div class="audioWrapper" v-if="pic.old && pic.type==2">
                            <img src="../../images/audioBg.png"/>
                            <div class="audioTime" flex="main:center cross:center">
                                <span v-text="pic.time>=60?parseInt(pic.time/60):0" flex-box="0"></span>
                                <span flex-box="0">:</span>
                                <span v-text="parseInt(pic.time%60)>=10?parseInt(pic.time%60):'0'+parseInt(pic.time%60)" flex-box="0"></span>
                            </div>
                        </div>
                        <img src="../../images/play.png" class="play" v-if="pic.old && pic.type==3"/>
                        <img src="../../images/play.png" class="play" v-if="!pic.old && pic.datatype==3"/>
                        <div class="removeIcon" @click.stop="removeItem(index)" v-if="canUploadClick"></div>
                    </li>
                </ul>
            </div>
            <div class="allSendBtn" :class="{'fullBtn':uploadList.length>=0}" flex="main:center cross:center" @click="send">保存</div>
        </div>
        <add-model ref="addModel" @addFuc="changeFile" :del="'audio'"></add-model>
        <div class="uploadWrapper" v-if="!canUploadClick"></div>
    </div>
</template>
<script type="text/babel">
    import API from '../../../api/api.js'
    import {domain,imagedomain} from '../../../api/api.js'
    import addModel from '../../../components/addModel.vue'
    const api = new API();
    export default{
        data(){
            return {
                title:new Date().Format('M月d日') +' 今日作品',
                content: '',
                uploadList: [],
                classId: this.$route.query.classId?this.$route.query.classId:'',
                opusId: this.$route.query.appId?this.$route.query.appId:'',
                studentId: this.$route.query.studentId?this.$route.query.studentId:'',
                domain: domain,
                imagedomain:imagedomain,
                imgKeys: [],
                canUploadClick: true,
                showList:false,
                upload:true,
                titleCheck:/[\ud800-\udbff][\udc00-\udfff]/
            }
        },
        mounted(){
            this.$util.jsBridgeCallBack('jsLeftAction', this.$util.goBack);
            this.$nextTick(function(){
                setTimeout(()=>{
                    this.propsTitle();
                },300)
                this.getData();
                document.documentElement.scrollTop=1;
                document.body.scrollTop=1;
            })
        },
        components:{
            addModel
        },
        methods: {
            closeWebView(){
                this.$util.JsBridge('closeWebView', {}, ()=> {})
            },
            showAdd(){
                this.$refs.addModel.addShow=true;
            },
            closeAdd(){
                this.$refs.addModel.addShow=false;
            },
            propsTitle(){
                this.$util.JsBridge('propsTitle', {
                    title: '今日作品',
                }, (res) => {
                })
            },
            getData(){
                let param = {
                    classId: this.classId,
                    opusId:this.opusId,
                    studentId:this.studentId
                };
                api.editPersonOpus(param).then(res => {
                    let {code, rs} = res;
                    if (code == 200) {
                        if(rs.opusPices){
                            this.uploadList=rs.opusPices;
                            this.uploadList.map((e)=>{
                                e.old=true
                            });
                         }
                         if(rs.stuOpus){
                             this.title=this.$util.entitiestoUtf16(rs.stuOpus.title).replace(/<br>/ig,"\n").replace(/&nbsp;/g, " ");
                             this.content=this.$util.entitiestoUtf16(rs.stuOpus.comment).replace(/<br>/ig,"\n").replace(/&nbsp;/g, " ");
                         }
                    }else{
                        this.$util.sendLog(1,'APP_OPUS',JSON.stringify({time:new Date().Format('yyyy/MM/dd hh:mm'),name:'今日作品',code:'APP_OPUS',path: '/app/opus/add',api:'editPersonOpus',success:false,data:res}));
                    }
                    this.$nextTick(function () {
                        document.documentElement.scrollTop=1;
                        document.body.scrollTop=1;
                    })
                }).catch(()=>{
                    this.$util.sendLog(1,'APP_OPUS',JSON.stringify({time:new Date().Format('yyyy/MM/dd hh:mm'),name:'今日作品',code:'APP_OPUS',path: '/app/opus/add',api:'editPersonOpus',success:false,data:'网络异常'}));
                });
            },
            getSwiper(index,pices){
                const oldArr=[];
                if(pices[index].old){
                    pices.forEach((e)=>{
                        if(e.old){
                            oldArr.push(e)
                        }
                    })
                    this.clientSwiper(oldArr,index)
                }else{
                    this.seePicesItem(index);
                }
            },
            seePicesItem(index){
                this.$util.JsBridge('seePicesItem', {
                    picItemIndex: index
                }, (res)=> {
                })
            },
            clientSwiper(pices, index){
                if(this.upload){
                    let arr = Array.prototype.toString.call(pices) === '[object String]' ? JSON.parse(JSON.stringify(JSON.parse(pices))) : JSON.parse(JSON.stringify(pices))
                    arr.forEach(function (e) {
                        e.imgUrl = imagedomain + e.imgUrl;
                        if (e.type == 3) {
                            e.coverUrl = imagedomain + e.imgUrl + '?vframe/jpeg/offset/1';
                        }
                    });
                    this.$util.JsBridge('picesSwiper', {
                        picesList: arr,
                        picesIndex: index
                    }, (res)=> {
                    })
                }else{
                    this.$toast('上传中,请勿点击操作');
                }
            },
            showPicesSwiper(array,index){
                let arr=[];
                array.forEach((el,i)=>{
                    if(!el.old){
                        let newEl={from:1,type:el.type,imgUrl:el.imgUrl,time:el.time};
                        arr.push(newEl);
                    }else{
                        let newEl={from:2,type:el.type,imgUrl:imagedomain +el.imgUrl,time:el.time};
                        if (el.type == 3) {
                            newEl.coverUrl = imagedomain + el.imgUrl + '?vframe/jpeg/offset/1';
                        }
                        arr.push(newEl);
                    }
                });
                this.$util.JsBridge('showPicesSwiper', {
                    picesList: arr,
                    picesIndex: index
                }, (res) => {
                })
            },
            removeItem(index){
                if(this.upload){
                    if(this.uploadList[index].old){}else{
                        let num=0;
                        this.uploadList.forEach((e)=>{
                            if(e.old){
                                num++;
                            }
                        })
                        this.$util.JsBridge('removePicesItem', {
                            picItemIndex: index-num
                        }, ()=> {})
                    }
                    this.uploadList.splice(index, 1)
                    this.upload=true;
                }else{
                    this.$toast('上传中,请勿点击操作');
                }
            },
            changeFile(type){
                //TODO:客户端上传文件
                if (9 - this.uploadList.length > 0) {
                    this.$util.JsBridge('getPicesList', {
                        picItemCount: 9 - this.uploadList.length,
                        showType: type,
                        appType: 'APP_OPUS'
                    }, (res)=> {
                        this.changeFileCallBack(res);
                        this.closeAdd();
                    })
                } else {
                    this.$toast('您最多只能上传9个附件');
                }
            },
            changeFileCallBack(res){
                this.imgKeys = res;
                for (let i = 0; i < res.length; i++) {
                    this.getImgInfo(res[i])
                }
            },
            getImgInfo(rs){
                this.$util.JsBridge('getImgInfo', {
                    fileKey: rs
                }, (res) => {
                    if (res.datathumbnail&&res.datathumbnail.indexOf('data:image/png;base64') == -1) {
                        res.datathumbnail = 'data:image/png;base64,' + res.datathumbnail;
                    }
                    res.imgUrl=rs;
                    res.type=res.datatype;
                    if(res.datatime){
                        res.time=res.datatime;
                    }
                    this.uploadList.push(res);
                })
            },
            check(){
                if (this.title==='') {
                    this.$toast('请输入作品标题');
                    return false
                } else if(this.titleCheck.test(this.title)){
                    this.$toast('标题内容不能包含表情');
                    return false;
                }else if (this.uploadList.length===0) {
                    this.$toast('请上传作品附件');
                    return false
                }
                return true
            },
            send(){
                if(!this.check()){
                    return;
                }
                if(this.canUploadClick){
                    this.canUploadClick=false;
                    let uploadCount=0;
                    let arr=[];
                    this.uploadList.forEach((e)=>{
                        if(e.old){
                            uploadCount++;
                        }
                        let item={
                            imgUrl:e.imgUrl,
                            type:e.type
                        };
                        if(e.time){
                            item.time=e.time;
                        }
                        arr.push(item);
                    });
                    let content = this.$util.utf16toEntities(this.content).replace(/\n|\r\n/g, "<br>").replace(/[ ]/g, "&nbsp;");
                    let param = {
                        studentId:this.studentId,
                        picCount:arr.length,
                        opusId:this.opusId,
                        classId:this.classId,
                        title:this.title,
                        comment:content,
                        pices:JSON.stringify(arr)
                    };
                    api.uploadPersonOpus(param).then((res)=> {
                        const {code}=res;
                        if (code == 200) {
                            if(!res.rs){
                                setTimeout(()=>{
                                    this.$toast('成功',true);
                                    this.$util.goBack();
                                },1000);
                                return;
                            }
                            this.$util.JsBridge('sendFinish', {
                                taskTid: res.rs,
                                appType: 'APP_OPUS',
                                content: this.content,
                                taskName: '作品',
                                taskImageURL: this.domain + 'www/static/images/app_icon_v200/jinrizuopin-3x.png'
                            }, () => {})
                        } else {
                            this.canUploadClick=true;
                            this.$util.sendLog(1,'APP_OPUS',JSON.stringify({time:new Date().Format('yyyy/MM/dd hh:mm'),name:'今日作品',code:'APP_OPUS',path: '/app/opus/add',api:'uploadPersonOpus',success:false,data:res}));
                        }
                    }).catch((e)=> {
                        this.canUploadClick=true;
                        this.$util.sendLog(1,'APP_OPUS',JSON.stringify({time:new Date().Format('yyyy/MM/dd hh:mm'),name:'今日作品',code:'APP_OPUS',path: '/app/opus/add',api:'uploadPersonOpus',success:false,data:'网络异常'}));
                    });
                }else {
                    this.$toast('上传中，请稍后');
                }
            },
        }
    }
</script>
<style scoped lang="scss">
    @keyframes float {
        0% {
            font-size:0.12rem;
            -webkit-transform: translate3d(0,0,0);
            transform: translate3d(0,0,0);
        }
        100% {
            font-size:0.1rem;
            -webkit-transform: translate3d(0,-0.2rem,0);
            transform: translate3d(0,-0.2rem,0);
        }
    }
    @keyframes floatOut {
        0% {
            font-size:0.1rem;
            -webkit-transform: translate3d(0,-0.2rem,0);
            transform: translate3d(0,-0.2rem,0);
        }
        100% {
            font-size:0.12rem;
            -webkit-transform: translate3d(0,0,0);
            transform: translate3d(0,0,0);
        }
    }
    .container {
        width: 100%;
        height: 100%;
        background: #000;
        position: relative;
    }
    .opusTitle{
        width:100%;
        background: transparent;
        padding:0 0.12rem;
        margin-top:0.21rem;
        margin-bottom:0.1rem;
        font-size: 0.12rem;
        color: #fff;
    }
    .bottomLine{
        background: rgba(255,255,255,.2);
        width:3.5rem;
        height:1px;
        padding:0 0.12rem;
        margin: 0 auto;
    }
    .inputModel {
        height: 1.83rem;
        width: 100%;
        padding-bottom: 0.13rem;
        border-bottom: 0.12rem solid rgba(0,0,0,0.5);
    }

    .inputTitle {
        padding: 0.12rem 0 0.12rem 0.12rem;
    }

    .inputLimit {
        opacity: 0.4;
        font-size: 0.1rem;
        text-align: right;
        margin-right: 0.12rem;
    }

    .titleInput {
        width: 100%;
        padding: 0 0.12rem;
        background: transparent;
        font-size: 0.13rem;
        color: #ffffff;
        height: 1rem;
    }

    .uploadTitle {
        padding: 0.17rem 0 0.12rem 0;
    }

    .uploadAdd {
        border: 1px dashed #ccd2da;
        width: 0.8rem;
        height: 0.8rem;
    }

    .uploadModel {
        padding-left:0.12rem;
    }

    .addIcon {
        width: 0.23rem;
        height: 0.23rem;
    }
    .picesWrap {
        background: #d8d8d8;
        position: relative;
        width: 0.8rem;
        height: 0.8rem;
        overflow: hidden;
        img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            -o-object-fit: cover;
        }
    }

    .uploadList {
        overflow: hidden;
        li {
            position: relative;
            margin-bottom: 0.1rem;
            margin-right: 0.1rem;
            float: left;
        }
    }

    .inputWrap {
        width: 2.7rem;
        height: 0.81rem;
        position: relative;
        background: rgba(250, 251, 255, .1);
        textArea {
            width: calc(100% - 0.24rem);
            height: 100%;
            background: transparent;
            padding: 0.08rem 0 0 0.08rem;
            font-size: 0.12rem;
            color: #fff;
        }
    }

    .removeIcon {
        width: 0.18rem;
        height: 0.18rem;
        position: absolute;
        right: 0.04rem;
        top: 0.04rem;
        background: url("../../images/delete.png") no-repeat center center/cover;
    }

    .uploadNum {
        opacity: 0.4;
        margin-left: 0.08rem;
    }

</style>
